#include <math.h>
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

class Figure
{
public:
    //两个纯虚函数（抽象类）
    virtual void display() const = 0;
    virtual double area() const = 0;
};

class Rectangle
: public Figure
{
public:
    Rectangle(double len, double wid)
    : _length(len)
    , _width(wid)
    {}

    void display() const override
    {
        cout << "Rectangle ";
    }

    double area() const override
    {
        return _length * _width;
    }

    ~Rectangle()
    {

    }

private:
    double _length;
    double _width;
};

class Circle
: public Figure
{
public:
    Circle(double rad)
    : _radius(rad)
    {}
 
    void display() const override
    {
        cout << "Circle ";
    }

    double area() const override
    {
        return 3.14 * _radius * _radius; 
    }

    ~Circle()
    {
    }

private:
    double _radius;
};

class Triangle
: public Figure
{
public:
    Triangle(double a,double b,double c)
    : _x(a)
    , _y(b)
    , _z(c)
    {}
  
    void display() const override
    {
        cout << "Triangle ";
    }

    //海伦公式
    double area() const override
    {
        //3 4 5 
        //c = 6
        //6 * 1 * 2 * 3
        double c = (_x + _y + _z)/2;
        return sqrt(c * (c - _x) * (c - _y) * (c - _z));
    }

    ~Triangle()
    {
    }
private:
    double _x;
    double _y;
    double _z;
};

void func(const Figure & fig)
{
    fig.display();
    cout << "的面积是：" << fig.area() << endl;
}

int main(void)
{
    Rectangle rectangle(10, 20);
    Circle circle(10);
    Triangle triangle(3, 4, 5);

    func(rectangle);
    func(circle);
    func(triangle);

    return 0;
}
